package com.yunao.service.impl;

import com.yunao.dao.TradeRepository;
import com.yunao.entity.Trade;
import com.yunao.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TradeServiceImpl implements TradeService {

    @Autowired
    private TradeRepository tradeRepository;

    @Override
    public List<Trade> findByParentid(Integer parentid) {
        return tradeRepository.findByParentid(parentid);
    }

    @Override
    public Trade findByName(String name) {
        return tradeRepository.findByName(name);
    }

    @Override
    public List<Map> findAll() {
        List<Map> maplist = new ArrayList<>();;
        List<Trade> firstTrades = tradeRepository.findByParentid(0);
        for (Trade trade:firstTrades){
            Map mapname = new HashMap();
            mapname.put("name",trade.getName());
            List<Trade> secondTrades = tradeRepository.findByParentid(trade.getId());
            List<String> list = new ArrayList<>();
            for (Trade secondRande : secondTrades){
                list.add(secondRande.getName());
            }
            mapname.put("subs",list);
            maplist.add(mapname);
        }
        return maplist;
    }

}
